<template>
	<view class="content">
		<image class="logo" src="../../static/logo.png" mode="aspectFill"></image>
	</view>
</template>

<script>
	import {
		getUserOpenId
	} from "../../utils/getOpenId";
	import {
		md5
	} from 'js-md5';

	import {
		getUserScoreFromCloud,
		saveUserCloudScoreToLocal
	} from '../../store/scoreManager.js'
	import {
		getTodayDateString,
		kAppToken,
		kAppKey
	} from '../../utils/common.js'
	import CryptoJS from '../../utils/cryptojs.js'

	// const iv = CryptoJS.enc.Utf8.parse(kAppKey);
	const iv = CryptoJS.enc.Utf8.parse("");
	// 导入云对象
	const appManager = uniCloud.importObject('appManager');
	export default {
		data() {
			return {
				title: 'Hello',
				usertype: 1,
			}
		},
		onShow(e) {
			// 先判断有无注册过用户名 如果注册过则为正常模式 
			// 正常模式userType = 2
			const username = uni.getStorageSync("kUsername")
			// 判断有无有户名 
			if (username && username.length > 0) {
				uni.setStorageSync("userType", 2);
				this.usertype = 2;
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/index/shouye' + '?usertype=' + this.usertype
					})
				}, 800)
			} else {
				// 如果userType !=1 则提示注册用户
				this.usertype = 1;
				uni.setStorageSync("userType", 1);
				let appVersion = "2.0.32";
				// 调用云对象方法
				appManager.getAppKey().then(res => {
					console.log("res: " + JSON.stringify(res));
					const decryptedData = CryptoJS.AES.decrypt(res.data, kAppToken, {
						iv: iv, // 使用上面定义的IV
						mode: CryptoJS.mode.CBC, // 加密模式设置为CBC
						padding: CryptoJS.pad.Pkcs7, // 填充方式设置为PKCS#7
					}).toString(CryptoJS.enc.Utf8);
					const body = JSON.parse(decryptedData);
					console.log("getAppKey-body: " + JSON.stringify(body));
					// 审核模式
					if (body.data && body.data.length > 0) {
						uni.setStorageSync("appKey", body.data[0]);
						if (body.data[0].version == appVersion) {
							uni.setStorageSync("userType", 1);
							this.usertype = 1;
							uni.reLaunch({
								url: '/pages/index/shouye' + '?usertype=' + this.usertype
							})
						} else {
							uni.setStorageSync("isNewUser", body.data[0].isNewUser);
							uni.setStorageSync("userType", 2);
							this.usertype = 2;
							this.loginUser();
						}
					} else {
						uni.setStorageSync("userType", 1);
						this.usertype = 1;
						uni.reLaunch({
							url: '/pages/index/shouye' + '?usertype=' + this.usertype
						})
					}
				}).catch(e => {
					console.log("e: " + JSON.stringify(e));
					uni.setStorageSync("userType", 1);
					this.usertype = 1;
					uni.reLaunch({
						url: '/pages/index/shouye' + '?usertype=' + this.usertype
					})
				})
			}
		},
		methods: {
			async loginUser() {
				let username = '';
				// #ifdef MP
				username = await getUserOpenId()
				// #endif
				// #ifdef APP
				username = uni.getSystemInfoSync().deviceId
				// #endif
				// 导入云对象
				// 使用AES算法、CBC模式和PKCS#7填充对明文进行加密
				const encryptedData = CryptoJS.AES.encrypt(username, kAppToken, {
					iv: iv, // 使用上面定义的IV
					mode: CryptoJS.mode.CBC, // 加密模式设置为CBC
					padding: CryptoJS.pad.Pkcs7, // 填充方式设置为PKCS#7
				}).toString();
				// 调用云对象方法
				appManager.loginUser({
					data: encryptedData
				}).then(res => {
					console.log("loginUserres: " + JSON.stringify(res));
					const body = res.data;
					if (body && body.length > 0) { // 已注册
						uni.setStorageSync("userid", body[0]._id)
						// #ifdef APP
						const isAgree = uni.getStorageSync('agree');
						console.log("isAgree:", isAgree);
						if (isAgree && isAgree == 1) {
							const vipdate = uni.getStorageSync('vipdate');
							if(vipdate && vipdate>0){
								const nowdate = new Date().getTime()
								if(vipdate>nowdate){
									uni.reLaunch({
										url: '/pages/index/shouye' + '?usertype=' + this.usertype
									})
								}else{
									uni.reLaunch({
										url: '/pages/index/inPurchase'
									})
								}
							}else{
								uni.reLaunch({
									url: '/pages/index/inPurchase'
								})
							}
						} else {
							// ios单独进行隐私政策验证
							uni.reLaunch({
								url: "/pages/index/privacy",
								success() {
									plus.navigator.closeSplashscreen()
								}
							})
						}
						// #endif

						// #ifdef MP
						
						uni.reLaunch({
							url: '/pages/index/shouye' + '?usertype=' + this.usertype
						})
						// #endif

					} else { // 未注册用户
						registerUser(username)
					}
				}).catch(e => {
					console.log(JSON.stringify(e));
					uni.setStorageSync("userType", 1);
					this.usertype = 1;
					uni.reLaunch({
						url: '/pages/index/shouye' + '?usertype=' + this.usertype
					})
				});
			},
		}
	}


	async function registerUser(openId) {
		const userid = md5(openId) ?? "临时用户";
		const userInfo = {
			username: openId,
			password: openId,
			nickname: userid,
			avatar: "https://mp-b4609b89-f010-4b4d-8ed3-0087451c83a0.cdn.bspapp.com/resource/icons/logo.png",
			score: 1000,
			todayscore: 0,
			todayscoredate: getTodayDateString()
		};
		// 导入云对象
		// 使用AES算法、CBC模式和PKCS#7填充对明文进行加密
		const encryptedData = CryptoJS.AES.encrypt(JSON.stringify(userInfo), kAppToken, {
			iv: iv, // 使用上面定义的IV
			mode: CryptoJS.mode.CBC, // 加密模式设置为CBC
			padding: CryptoJS.pad.Pkcs7, // 填充方式设置为PKCS#7
		}).toString();
		appManager.registerUser({
			data: encryptedData
		}).then(info => {
			console.log("注册成功", JSON.stringify(userInfo));
			uni.hideLoading()
			uni.setStorageSync("kUsername", openId);
			uni.setStorageSync("userType", 2);
			this.usertype = 2;
			setTimeout(() => {
				// #ifdef MP
				uni.reLaunch({
					url: '/pages/index/shouye' + '?usertype=' + this.usertype
				})
				// #endif

				// #ifdef APP
				const isAgree = uni.getStorageInfoSync('Agree');
				if (isAgree && isAgree == 1) {
					const vipdate = uni.getStorageSync('vipdate');
					if(vipdate && vipdate>0){
						const nowdate = new Date().getTime()
						if(vipdate>nowdate){
							uni.reLaunch({
								url: '/pages/index/shouye' + '?usertype=' + this.usertype
							})
						}else{
							uni.reLaunch({
								url: '/pages/index/inPurchase'
							})
						}
					}
				} else {
					// ios单独进行隐私政策验证
					uni.reLaunch({
						url: "/pages/index/privacy",
						success() {
							plus.navigator.closeSplashscreen()
						}
					})
				}
				// #endif
			}, 200)
		}).catch(e => {
			uni.setStorageSync("userType", 1);
			this.usertype = 1;
			uni.reLaunch({
				url: '/pages/index/shouye' + '?usertype=' + this.usertype
			})
		})
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.text-area {
		display: flex;
		justify-content: center;
		color: gold;
		font-size: 100rpx;
		font-weight: bold;
		width: 100%;
		text-align: center;
	}

	.logo {
		height: 250rpx;
		width: 250rpx;
		margin-top: 30vh;
		border-radius: 50%;
	}
</style>